package in.srain.cube.app.lifecycle;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.ConditionVariable;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import in.srain.cube.util.CLog;

public class AppActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    public static final String TAG = "AppActivityLifecycleCallbacks";
    private static final AppActivityLifecycleCallbacks INSTANCE = new AppActivityLifecycleCallbacks();
    private static final ConditionVariable FOREGROUND_CONDITION = new ConditionVariable(true);

    private int mActivityActiveCount = 0;
    private List<OnLifecycleListener> mOnLifecycleListenerList;

    private AppActivityLifecycleCallbacks() {
    }

    public static AppActivityLifecycleCallbacks getInstance() {
        return INSTANCE;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        mActivityActiveCount++;
        if (mActivityActiveCount == 1) {
            CLog.d(TAG, "appStart");
            onAppToForeground();
            if (mOnLifecycleListenerList != null && !mOnLifecycleListenerList.isEmpty()) {
                for (OnLifecycleListener onLifecycleListener : mOnLifecycleListenerList) {
                    onLifecycleListener.onAppToForeground();
                }
            }
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        mActivityActiveCount--;
        if (mActivityActiveCount == 0) {
            CLog.d(TAG, "appStop");
            onAppToBackground();
            if (mOnLifecycleListenerList != null && !mOnLifecycleListenerList.isEmpty()) {
                for (OnLifecycleListener onLifecycleListener : mOnLifecycleListenerList) {
                    onLifecycleListener.onAppToBackground();
                }
            }
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    public boolean isAppInForeground() {
        return mActivityActiveCount > 0;
    }

    private void onAppToForeground() {
        FOREGROUND_CONDITION.open();
    }

    private void onAppToBackground() {
        FOREGROUND_CONDITION.close();
    }

    public void waitIfAppInBackground() {
        FOREGROUND_CONDITION.block();
    }

    public boolean waitIfAppInBackground(long timeout) {
        return FOREGROUND_CONDITION.block(timeout);
    }

    public void addLifecycleListener(OnLifecycleListener onLifecycleListener) {
        if (mOnLifecycleListenerList == null) {
            mOnLifecycleListenerList = new CopyOnWriteArrayList<>();
        }
        mOnLifecycleListenerList.add(onLifecycleListener);
    }

    public void removeLifecycleListener(OnLifecycleListener onLifecycleListener) {
        if (mOnLifecycleListenerList == null) {
            return;
        }
        mOnLifecycleListenerList.remove(onLifecycleListener);
        if (mOnLifecycleListenerList.size() == 0) {
            mOnLifecycleListenerList = null;
        }
    }

    public interface OnLifecycleListener {

        void onAppToForeground();

        void onAppToBackground();
    }
}
